package smallChanges.lineNumberMapping;

import smallChanges.statements.FieldDeclarationStatement;

public class RenamedFieldComparison implements RenamedSourceObjectComparison {

	private double comparisonStatistic; 
	
	public RenamedFieldComparison(FieldDeclarationStatement leftDecl, FieldDeclarationStatement rightDecl)
	{
		comparisonStatistic = 0.0;
		boolean typeMatch = false;
		double nameDist;
		
		if( leftDecl.getVisibility() == rightDecl.getVisibility())
		{
			comparisonStatistic += 0.25;
		}
		if( leftDecl.getModifiers().isFinal() == rightDecl.getModifiers().isFinal() )
		{
			comparisonStatistic += 0.25;
		}
		if( leftDecl.getModifiers().isStatic() == rightDecl.getModifiers().isStatic() )
		{
			comparisonStatistic += 0.25;			
		}
		if( leftDecl.getModifiers().isVolatile() == rightDecl.getModifiers().isVolatile() )
		{
			comparisonStatistic += 0.25;
		}
		if( leftDecl.getModifiers().isTransient() == rightDecl.getModifiers().isTransient() )
		{
			comparisonStatistic += 0.25;	
		}
		if( leftDecl.getTypeAsString() != null && rightDecl.getTypeAsString() != null && leftDecl.getTypeAsString().equals(rightDecl.getTypeAsString()))
		{
			typeMatch = true;
            comparisonStatistic += 1.0;    
		}
		nameDist = org.apache.commons.lang.StringUtils.getLevenshteinDistance(leftDecl.getName(), rightDecl.getName()) /
			((double)Math.max(leftDecl.getName().length(), rightDecl.getName().length()));
 
		comparisonStatistic += 2 * (1 - nameDist);
		
		if( nameDist == 0.0 && typeMatch)
		{
			// if type and name match exactly, we have a match!
			comparisonStatistic = getComparisonStatisticMax();
		}
	}


	public double getComparisonStatistic() {
		// TODO Auto-generated method stub
		return comparisonStatistic;
	}


	public double getComparisonStatisticMax() {
		// TODO Auto-generated method stub
		return 4.25;
	}

}
